package org.gilgamesh.stool.core.time;

import cn.hutool.core.date.*;
import java.util.Date;

/**
 * 时间工具类
 *
 * @author Gilgamesh
 * @since 2021-08-18
 **/
public class TimeUtil {

    /**
     * 获取当前时间戳
     *
     * @author Gilgamesh
     * @since 2021-08-18
     **/
    public static long current() {
        return DateUtil.current();
    }

    /**
     * 当天的结束时间戳
     *
     * @author Gilgamesh
     * @since 2021-08-18
     **/
    public static long todayEnd() {
        return DateUtil.endOfDay(new Date()).getTime();
    }

    /**
     * 获取今天的开始的时间戳
     *
     * @author Gilgamesh
     * @since 2021-08-18
     **/
    public static long todayStart() {
        return DateUtil.beginOfDay(new Date()).getTime();
    }

    /**
     * 昨天的开始时间戳
     *
     * @author Gilgamesh
     * @since 2021-08-18
     **/
    public static long yesterdayStart() {
        return DateUtil.beginOfDay(DateUtil.yesterday()).getTime();
    }

    /**
     * 昨天的结束时间戳
     *
     * @author Gilgamesh
     * @since 2021-08-18
     **/
    public static long yesterdayEnd() {
        return DateUtil.endOfDay(DateUtil.yesterday()).getTime();
    }

    /**
     * 本周的开始时间戳
     *
     * @param isMondayAsFirstDay 周一是否为一周的第一天
     * @author Gilgamesh
     * @since 2021-08-18
     **/
    public static long curWeekStart(boolean isMondayAsFirstDay) {
        return DateUtil.beginOfWeek(new Date(), isMondayAsFirstDay).getTime();
    }

    /**
     * 本周的结束时间戳
     *
     * @param isMondayAsFirstDay 周一是否为一周的第一天
     * @author Gilgamesh
     * @since 2021-08-18
     **/
    public static long curWeekEnd(boolean isMondayAsFirstDay) {
        return DateUtil.endOfWeek(new Date(), isMondayAsFirstDay).getTime();
    }

    /**
     * 上周的开始时间戳
     *
     * @param isMondayAsFirstDay 周一是否为一周的第一天
     * @author Gilgamesh
     * @since 2021-08-18
     **/
    public static long lastWeekStart(boolean isMondayAsFirstDay) {
        return DateUtil.beginOfWeek(DateUtil.lastWeek(), isMondayAsFirstDay).getTime();
    }

    /**
     * 上周的结束时间戳
     *
     * @param isMondayAsFirstDay 周一是否为一周的第一天
     * @author Gilgamesh
     * @since 2021-08-18
     **/
    public static long lastWeekEnd(boolean isMondayAsFirstDay) {
        return DateUtil.endOfWeek(DateUtil.lastWeek(), isMondayAsFirstDay).getTime();
    }

    /**
     * 本月的开始时间戳
     *
     * @author Gilgamesh
     * @since 2021-08-18
     **/
    public static long curMonthStart() {
        return DateUtil.beginOfMonth(new Date()).getTime();
    }

    /**
     * 本月的结束时间戳
     *
     * @author Gilgamesh
     * @since 2021-08-18
     **/
    public static long curMonthEnd() {
        return DateUtil.endOfMonth(new Date()).getTime();
    }

    /**
     * 上个月的开始时间戳
     *
     * @author Gilgamesh
     * @since 2021-08-18
     **/
    public static long lastMonthStart() {
        return DateUtil.beginOfMonth(DateUtil.lastMonth()).getTime();
    }

    /**
     * 上个月的结束时间戳
     *
     * @author Gilgamesh
     * @since 2021-08-18
     **/
    public static long lastMonthEnd() {
        return DateUtil.endOfMonth(DateUtil.lastMonth()).getTime();
    }

    /**
     * 今年的开始时间戳
     *
     * @author Gilgamesh
     * @since 2021-08-18
     **/
    public static long curYearStart() {
        return DateUtil.beginOfYear(new Date()).getTime();
    }

    /**
     * 今年的结束时间戳
     *
     * @author Gilgamesh
     * @since 2021-08-18
     **/
    public static long curYearEnd() {
        return DateUtil.endOfYear(new Date()).getTime();
    }

    /**
     * 去年的开始时间戳
     *
     * @author Gilgamesh
     * @since 2021-08-18
     **/
    public static long lastYearStart() {
        return DateUtil.beginOfYear(lastYear()).getTime();
    }

    /**
     * 今年的结束时间戳
     *
     * @author Gilgamesh
     * @since 2021-08-18
     **/
    public static long lastYearEnd() {
        return DateUtil.endOfYear(lastYear()).getTime();
    }

    /**
     * 去年
     *
     * @author Gilgamesh
     * @since 2021-08-18
     **/
    public static DateTime lastYear() {
        return DateUtil.offset(new Date(), DateField.YEAR, -1);
    }

    /**
     * 上一年
     *
     * @author Gilgamesh
     * @since 2021-08-18
     **/
    public static DateTime lastYear(Date date) {
        return DateUtil.offset(date, DateField.YEAR, -1);
    }

    /**
     * 某一天的时间戳
     *
     * @author Gilgamesh
     * @since 2021-08-18
     **/
    public static long time(Date date) {
        return date.getTime();
    }

    /**
     * 获取季度
     *
     * @return Q1:第一季度，Q2:第二季度，以此类推
     * @author Gilgamesh
     * @since 2021-08-18
     **/
    public static Quarter quarter(Date date) {
        return DateUtil.quarterEnum(date);
    }

    /**
     * 获取当前季度
     *
     * @return Q1:第一季度，Q2:第二季度，以此类推
     * @author Gilgamesh
     * @since 2021-08-18
     **/
    public static Quarter quarter() {
        return DateUtil.quarterEnum(new Date());
    }

    /**
     * 获取当前季度的开始时间
     *
     * @return {@link DateTime}
     * @author Gilgamesh
     * @since 2021-08-18
     **/
    private static DateTime quarterStart() {
        return DateUtil.beginOfQuarter(new Date());
    }

    /**
     * 获取当前季度的结束时间
     *
     * @return {@link DateTime}
     * @author Gilgamesh
     * @since 2021-08-18
     **/
    private static DateTime quarterEnd() {
        return DateUtil.endOfQuarter(new Date());
    }

    /**
     * 获取上个季度的开始时间
     *
     * @return {@link DateTime}
     * @author Gilgamesh
     * @since 2021-08-18
     **/
    private static DateTime lastQuarterStartDate() {
        DateTime time = DateUtil.offset(new Date(), DateField.MONTH, -3);
        return DateUtil.beginOfQuarter(time);
    }

    /**
     * 获取上个季度的结束时间
     *
     * @return {@link DateTime}
     * @author Gilgamesh
     * @since 2021-08-18
     **/
    private static DateTime lastQuarterEndDate() {
        DateTime time = DateUtil.offset(new Date(), DateField.MONTH, -3);
        return DateUtil.endOfQuarter(time);
    }

    /**
     * 获取上个季度的开始时间
     *
     * @return {@link DateTime}
     * @author Gilgamesh
     * @since 2021-08-18
     **/
    private static DateTime lastQuarterStartDate(Date date) {
        DateTime time = DateUtil.offset(date, DateField.MONTH, -3);
        return DateUtil.beginOfQuarter(time);
    }

    /**
     * 获取上个季度的结束时间
     *
     * @return {@link DateTime}
     * @author Gilgamesh
     * @since 2021-08-18
     **/
    private static DateTime lastQuarterEndDate(Date date) {
        DateTime time = DateUtil.offset(date, DateField.MONTH, -3);
        return DateUtil.endOfQuarter(time);
    }

    /**
     * 当前季度开始的时间戳
     *
     * @author Gilgamesh
     * @since 2021-08-18
     **/
    public static long curQuarterStart() {
        return quarterStart().getTime();
    }

    /**
     * 当前季度的结束时间戳
     *
     * @author Gilgamesh
     * @since 2021-08-18
     **/
    public static long curQuarterEnd() {
        return quarterEnd().getTime();
    }

    /**
     * 当前季度开始的时间戳
     *
     * @author Gilgamesh
     * @since 2021-08-18
     **/
    public static long lastQuarterStart() {
        return lastQuarterStartDate().getTime();
    }

    /**
     * 当前季度的结束时间戳
     *
     * @author Gilgamesh
     * @since 2021-08-18
     **/
    public static long lastQuarterEnd() {
        return lastQuarterEndDate().getTime();
    }

    /**
     * 某一天的开始
     *
     * @author Gilgamesh
     * @since 2021-08-18
     **/
    public static long dayStart(Date date) {
        return DateUtil.beginOfDay(date).getTime();
    }

    /**
     * 某一天的结束
     *
     * @author Gilgamesh
     * @since 2021-08-18
     **/
    public static long dayEnd(Date date) {
        return DateUtil.endOfDay(date).getTime();
    }

    /**
     * 某一周的开始
     *
     * @author Gilgamesh
     * @since 2021-08-18
     **/
    public static long weekStart(Date date, boolean isSundayAsLastDay) {
        return DateUtil.beginOfWeek(date, isSundayAsLastDay).getTime();
    }

    /**
     * 某一周的结束
     *
     * @author Gilgamesh
     * @since 2021-08-18
     **/
    public static long weekEnd(Date date, boolean isSundayAsLastDay) {
        return DateUtil.endOfWeek(date, isSundayAsLastDay).getTime();
    }

    /**
     * 某一月的开始
     *
     * @author Gilgamesh
     * @since 2021-08-18
     **/
    public static long monthStart(Date date) {
        return DateUtil.beginOfMonth(date).getTime();
    }

    /**
     * 某一月的结束
     *
     * @author Gilgamesh
     * @since 2021-08-18
     **/
    public static long monthEnd(Date date) {
        return DateUtil.endOfMonth(date).getTime();
    }

    /**
     * 某一年的开始
     *
     * @author Gilgamesh
     * @since 2021-08-18
     **/
    public static long yearStart(Date date) {
        return DateUtil.beginOfYear(date).getTime();
    }

    /**
     * 某一年的结束
     *
     * @author Gilgamesh
     * @since 2021-08-18
     **/
    public static long yearEnd(Date date) {
        return DateUtil.endOfYear(date).getTime();
    }

    /**
     * 某一季度的开始
     *
     * @author Gilgamesh
     * @since 2021-08-18
     **/
    public static long quarterStart(Date date) {
        return DateUtil.beginOfQuarter(date).getTime();
    }

    /**
     * 某一季度的结束
     *
     * @author Gilgamesh
     * @since 2021-08-18
     **/
    public static long quarterEnd(Date date) {
        return DateUtil.endOfQuarter(date).getTime();
    }

    /**
     * 时间转字符串
     *
     * @param time    long时间戳
     * @param pattern 时间格式 {@link DatePattern}
     * @author Gilgamesh
     * @since 2021-08-18
     **/
    public static String format(long time, String pattern) {
        return DateUtil.format(new Date(time), pattern);
    }

    /**
     * 时间转字符串
     *
     * @param date    时间对象
     * @param pattern 时间格式 {@link DatePattern}
     * @author Gilgamesh
     * @since 2021-08-18
     **/
    public static String format(Date date, String pattern) {
        return DateUtil.format(date, pattern);
    }
}
